/*
 * @lc app=leetcode.cn id=67 lang=cpp
 *
 * [67] 二进制求和
 *
 * https://leetcode-cn.com/problems/add-binary/description/
 *
 * algorithms
 * Easy (54.40%)
 * Likes:    599
 * Dislikes: 0
 * Total Accepted:    162K
 * Total Submissions: 297.7K
 * Testcase Example:  '"11"\n"1"'
 *
 * 给你两个二进制字符串，返回它们的和（用二进制表示）。
 *
 * 输入为 非空 字符串且只包含数字 1 和 0。
 *
 *
 *
 * 示例 1:
 *
 * 输入: a = "11", b = "1"
 * 输出: "100"
 *
 * 示例 2:
 *
 * 输入: a = "1010", b = "1011"
 * 输出: "10101"
 *
 *
 *
 * 提示：
 *
 *
 * 每个字符串仅由字符 '0' 或 '1' 组成。
 * 1 <= a.length, b.length <= 10^4
 * 字符串如果不是 "0" ，就都不含前导零。
 *
 *
 */

// @lc code=start
class Solution {
  public:
    string addBinary(string a, string b) {
        int ssize = min(a.size(), b.size());
        int bsize = max(a.size(), b.size());
        int low = ssize;
        int high = bsize - ssize;
        string solu;
        int j = 0;
        int temp = '\0';
        for (int i = low - 1; i >= 0; --i) {
            temp = a[i] + b[i + high] - '0' + j;
            j = (temp - '0') / 2;
            temp = (temp - '0') % 2 + '0';
            solu.push_back(temp);
        }
        string c;
        if (a.size() > b.size())
            c = a;
        else
            c = b;
        for (int i = high - 1; i >= 0; --i) {
            temp = c[i] + j;
            j = (temp - '0') / 2;
            temp = (temp - '0') % 2 + '0';
            solu.push_back(temp);
        }
        if (j == 1)
            solu.push_back('1');
        solu.assign(solu.rbegin(), solu.rend());
        return solu;
    }
};
// @lc code=end
